//
//  MYGResumeHeaderView.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/4/12.
//

import UIKit
import QMUIKit

class MYGResumeHeaderView: UIView {
    var CNPersonInfoBlock:(()->Void)?
    var ENPersonInfoBlock:(()->Void)?
    var mediaPersonInfoBlock:(()->Void)?
    var isEN:Bool = false {
        didSet {
            guard let item = model else{return}
            
            icon.sd_setImage(with: URL(string: item.avatar), placeholderImage: UIImage(named: "head"))
            let name = isEN ? item.en_name : item.name
            let hospital = isEN ? item.en_hospital : item.hospital
            let department = isEN ? item.en_department : item.department
            nameLabel.rz.colorfulConfer { confer in
                confer.text("\(name)  ")?.font(.medium(20)).textColor(.rgba(39, 39, 39, 1))
                confer.text("\(hospital)-\(department)")?.font(.regular(13)).textColor(.rgba(77, 77, 77, 1))
            }
            let email = isEN ? item.en_email :item.email
            phoneView.setTitle("电话：\(item.phone)", for: .normal)
            emailView.setTitle("邮箱：\(email)", for: .normal)
        }
    }
    var model:MYGUserDoctor? {
        
        didSet {
            guard let item = model else{return}
            
            icon.sd_setImage(with: URL(string: item.avatar), placeholderImage: UIImage(named: "head"))
            let name = isEN ? item.en_name : item.name
            let hospital = isEN ? item.en_hospital : item.hospital
            nameLabel.rz.colorfulConfer { confer in
                confer.text("\(name)  ")?.font(.medium(20)).textColor(.rgba(39, 39, 39, 1))
                confer.text("\(hospital)-\(item.department)")?.font(.regular(13)).textColor(.rgba(77, 77, 77, 1))
            }
            let email = isEN ? item.en_email :item.email
            phoneView.setTitle("电话：\(item.phone)", for: .normal)
            emailView.setTitle("邮箱：\(email)", for: .normal)
        }
        
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        configUI()
        CNButton.isSelected = true
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    // MARK: - UI
    private func configUI() {
        backgroundColor = .white
        
        addSubview(icon)
        addSubview(CNButton)
        addSubview(ENButton)
        addSubview(MButton)
        
        addSubview(nameLabel)
        addSubview(phoneView)
        addSubview(emailView)
        addSubview(line)
        icon.snp.makeConstraints { make in
            make.left.top.equalTo(33)
            make.width.height.equalTo(64)
        }
        
        MButton.snp.makeConstraints { make in
            make.right.equalToSuperview().inset(16)
            make.height.equalTo(19)
            make.width.equalTo(35)
            make.centerY.equalTo(icon)
        }
        
        ENButton.snp.makeConstraints { make in
            make.right.equalTo(MButton.snp.left)
            make.width.height.centerY.equalTo(MButton)
        }
        
        CNButton.snp.makeConstraints { make in
            make.right.equalTo(ENButton.snp.left)
            make.width.height.centerY.equalTo(MButton)
        }
        
        nameLabel.snp.makeConstraints { make in
            make.left.equalTo(icon)
            make.top.equalTo(icon.snp.bottom).offset(13)
            make.height.equalTo(28)
        }
        phoneView.snp.makeConstraints { make in
            make.left.equalTo(icon)
            make.top.equalTo(nameLabel.snp.bottom).offset(14)
            make.height.equalTo(15)
        }
        emailView.snp.makeConstraints { make in
            make.left.equalTo(icon)
            make.top.equalTo(phoneView.snp.bottom).offset(14)
            make.height.equalTo(15)
        }
        
        line.snp.makeConstraints { make in
            make.left.right.equalToSuperview().inset(26)
            make.height.equalTo(0.5)
            make.bottom.equalToSuperview()
        }
        
    }
     
    private lazy var icon:UIImageView = {
        let iv = UIImageView()
        iv.image = UIImage(named: "head")
        iv.layer.masksToBounds = true
        iv.layer.cornerRadius = 32
        return iv
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(20)
        label.textColor = .rgba(39, 39, 39, 1)
//        label.rz.colorfulConfer { confer in
//            confer.text("乔淑琴 ")?.font(.medium(20)).textColor(.rgba(39, 39, 39, 1))
//            confer.text(" 正安县人民医院-医生助理")?.font(.regular(13)).textColor(.rgba(77, 77, 77, 1))
//        }
        return label
    }()
    

    private lazy var phoneView: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "re_phone"), for: .normal)
        btn.spacingBetweenImageAndTitle = 8
        btn.imagePosition = .left
        btn.setTitle("电话：", for: .normal)
        btn.setTitleColor(.rgba(85, 85, 85, 1), for: .normal)
        btn.titleLabel?.font = .regular(14)
        return btn
    }()
    private lazy var emailView: QMUIButton = {
        let btn = QMUIButton()
        btn.setImage(UIImage(named: "resum_email"), for: .normal)
        btn.spacingBetweenImageAndTitle = 8
        btn.imagePosition = .left
        btn.setTitle("邮箱：", for: .normal)
        btn.setTitleColor(.rgba(85, 85, 85, 1), for: .normal)
        btn.titleLabel?.font = .regular(14)
        return btn
    }()
    
    
    private lazy var CNButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("中文", for: .normal)
        btn.setTitleColor(.rgba(107, 107, 107, 1), for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.setBackgroundColor(color: .rgba(77, 107, 253, 1), forState: .selected)
        btn.titleLabel?.font = .regular(11)
        btn.layer.cornerRadius = 3
        btn.layer.borderWidth = 0.5
        btn.addTarget(self, action: #selector(clickBtn(_:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var ENButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("英文", for: .normal)
        btn.setTitleColor(.rgba(107, 107, 107, 1), for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.setBackgroundColor(color: .rgba(77, 107, 253, 1), forState: .selected)
        btn.titleLabel?.font = .regular(11)
        btn.layer.cornerRadius = 3
        btn.layer.borderWidth = 0.5
        btn.addTarget(self, action: #selector(clickBtn(_:)), for: .touchUpInside)
        return btn
    }()
    
    private lazy var MButton: QMUIButton = {
        let btn = QMUIButton()
        btn.setTitle("媒体", for: .normal)
        btn.setTitleColor(.rgba(107, 107, 107, 1), for: .normal)
        btn.setTitleColor(.white, for: .selected)
        btn.setBackgroundColor(color: .rgba(77, 107, 253, 1), forState: .selected)
        btn.titleLabel?.font = .regular(11)
        btn.layer.cornerRadius = 3
        btn.layer.borderWidth = 0.5
        btn.addTarget(self, action: #selector(clickBtn(_:)), for: .touchUpInside)
        return btn
    }()
    
    
    private lazy var line:UIView = {
        let line = UIView()
        line.backgroundColor = .rgba(238, 238, 238, 1)
        return line
        
    }()
    
}

extension MYGResumeHeaderView {
    
    @objc func clickBtn(_ sender:UIButton) {
        
        if sender == CNButton {
            CNButton.isSelected = true
            ENButton.isSelected = false
            MButton.isSelected = false
            CNPersonInfoBlock?()
        }else if sender == ENButton {
            CNButton.isSelected = false
            ENButton.isSelected = true
            MButton.isSelected = false
            ENPersonInfoBlock?()
        }else {
            CNButton.isSelected = false
            ENButton.isSelected = false
            MButton.isSelected = true
            mediaPersonInfoBlock?()
        }
    }
}
